package org.lee.sc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * Client
 * 客户端
 *
 * @author dralee
 * @date 2021-12-28 18:06:44
 **/
public class Client {
    public static void main(String[] args) {
        int port = 12321;
        try (Socket socket = new Socket(InetAddress.getLocalHost().getHostAddress(), port)) {
            try (PrintWriter writer = new PrintWriter(socket.getOutputStream(), true)) {
                try (Scanner scanner = new Scanner(System.in)) {
                    try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
                        String cmd;
                        do {
                            System.out.print("Enter command:");
                            cmd = scanner.nextLine();
                            writer.println(cmd);
                            String line = reader.readLine();
                            System.out.println(line);
                        } while (!"byte".equals(cmd) && !"quit".equals(cmd));
                    }
                }
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
